package com.vgemv.jsutilitysdk.activity.jsloglistactivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.vgemv.jsutilitysdk.Application.JsBaseApplication;
import com.vgemv.jsutilitysdk.Display.JsActivityContentDisplayManager.JsActivityContentDisplayManager;
import com.vgemv.jsutilitysdk.Email.JsEmailManager;
import com.vgemv.jsutilitysdk.Email.JsEmailSendCallback;
import com.vgemv.jsutilitysdk.log.JsLogManager;
import com.vgemv.jsutilitysdk.R;
import com.vgemv.jsutilitysdk.file.JsFileManager;
import com.vgemv.jsutilitysdk.map.JsMapManager;

import java.io.File;
import java.util.Map;

public class JsLogDetailActivity extends AppCompatActivity {

    RelativeLayout mRl_root = null;
    private File mFile;
    private TextView mTv;
    private ScrollView mSv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        JsLogFileItemModel model = (JsLogFileItemModel) JsLogListActivity.mLogFileItemVm.currentModel();
        mFile = model.mFile;
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(model.getFileName());

        mRl_root = new RelativeLayout(this);
        setContentView(mRl_root);

        mSv = new ScrollView(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mRl_root.addView(mSv);

        mTv = new TextView(this);
        lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mSv.addView(mTv);


        mTv.setText(model.getFileContext());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem_share = menu.add("分享");
        menuItem_share.setEnabled(true);
        menuItem_share.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menuItem_share.setIcon(R.mipmap.share_stroke_8a8a8a);
        menuItem_share.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(@NonNull MenuItem menuItem) {
                JsLogManager.logfc_d("JsLogDetailActivity","click share item.");
                //分享的时候需要加上系统相关的环境信息.
                Map deviceInfoMap = JsBaseApplication.sharedInstance().deviceInfo();
                String str_info = JsMapManager.prettyString(deviceInfoMap);
                JsLogManager.logfc_v(null,"Device info:%s",str_info);
                shareFile(mFile);
                return false;
            }
        });

        MenuItem menuItem_email = menu.add("邮件发送至开发者");
        menuItem_email.setEnabled(true);
        menuItem_email.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        menuItem_email.setIcon(R.mipmap.email);
        menuItem_email.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(@NonNull MenuItem menuItem) {
                JsLogManager.logfc_d("JsLogDetailActivity","click share item.");
                JsActivityContentDisplayManager.sharedInstance().displayMessage("发送系统日志...",JsLogDetailActivity.this);
                //subject [appName][系统日志][日志name]
                String subject = String.format("[%s][%s][%s]",JsBaseApplication.sharedInstance().getAppName(),"系统日志",mFile.getName());
                //context 相关信息.
                Map deviceInfoMap = JsBaseApplication.sharedInstance().deviceInfo();
                String context = JsMapManager.prettyString(deviceInfoMap);
                boolean isDebug = JsBaseApplication.sharedInstance().isDebug();
                JsEmailManager.sendMail(mFile.getAbsolutePath(), subject, context, isDebug, new JsEmailSendCallback() {
                    @Override
                    public void onMailSendSuccess() {
                        JsActivityContentDisplayManager.sharedInstance().displayMessage("日志发送成功!",JsLogDetailActivity.this);
                    }

                    @Override
                    public void onMailSendFail() {
                        JsActivityContentDisplayManager.sharedInstance().displayMessage("日志发送失败!",JsLogDetailActivity.this);
                    }
                });
                return false;
            }
        });
        return true;
    }

    private void shareFile(File file) {
        final Uri uri;
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= 24) {
            //若SDK大于等于24  获取uri采用共享文件模式
            //Environment.getExternalStorageDirectory().getPath());
            //如果没有为这个文件进行配置,就得不到uri.
            String applicationId = JsBaseApplication.sharedInstance().getPackageName();
            String authorities = String.format("%s.fileprovider",applicationId);
            JsLogManager.logfc_v(null,"authorities is: %s",authorities);
            uri = FileProvider.getUriForFile(this.getApplicationContext(), authorities, file);
        } else {
            uri = Uri.fromFile(file);
        }

        Intent share = new Intent(Intent.ACTION_SEND);
        share.putExtra(Intent.EXTRA_STREAM, uri);
        //此处可发送多种文件,会弹出可以处理的app来接受.
        /*"application/pdf"*/
        String str_type = JsFileManager.getMIMEType(file);
        share.setType(str_type);
        share.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        share.addCategory(Intent.CATEGORY_DEFAULT);
//        share.setPackage("com.tencent.mobileqq");
//若需要分享到微信，只需要更改Package即可
//share.setPackage("com.tencent.mm")
        if (share.resolveActivity(JsLogDetailActivity.this.getPackageManager()) != null)
        {
            JsLogDetailActivity.this.startActivity(share);
        } else {
//            ToastUtils.show("没有可以处理该pdf文件的应用");
        }
    }
}
